package com.doordu.framework.cloud.xxl.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

/**
 * 未来计划打算使用更加完善的方式扩展xxljob框架
 *
 * @author zxw
 * @date 2020/9/21 22:49
 */
//@Configuration
@Deprecated
public class XxlJobConfiguration implements EnvironmentAware {
    private Environment env;

    @Bean
    public XxlJobSpringExecutor xxlJobSpringExecutor() {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        String adminAddresses = getProperty("doordu.job.adminAddresses");
        String accessToken = getProperty("doordu.job.accessToken");
        String appname = getProperty("doordu.job.appname");
        String address = getProperty("doordu.job.address");
        int port = 0;
        int logRetentionDays = 0;
        String ip = getProperty("doordu.job.ip");
        String portString = getProperty("doordu.job.port");
        String logPath = getProperty("doordu.job.logPath");
        String daysString = getProperty("doordu.job.logRetentionDays");
        if(StringUtils.isNotBlank(ip)){
            port = Integer.parseInt(portString);
        }
        if(StringUtils.isNotBlank(daysString)){
            logRetentionDays = Integer.parseInt(daysString);
        }
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setAddress(address);
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        xxlJobSpringExecutor.setPort(port);
        return xxlJobSpringExecutor;
    }

    public String getProperty(String property) {
        return env.containsProperty(property) ? env.getProperty(property) : "";
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}
